/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
//Simbolo  String
//Prefijo o sufijo
package bsc.war.form;

import bsc.ejb.entity.Comparator;
import bsc.ejb.entity.Pointer;
import bsc.ejb.entity.ScoreCard;
import bsc.ejb.entity.Settings;
import bsc.ejb.metadata.Comparation;
import bsc.ejb.metadata.Framework;
import bsc.war.delegate.DelegateBSC;
import bsc.war.factory.Factory;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 *
 * @author luisZavaleta
 */
public class NewComparation extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        boolean edit = false;
        Long comparationId = -1L;

        if (NumberUtils.isDigits(request.getParameter("id"))) {
            Long id = Long.valueOf(request.getParameter("id"));
            if (id >= 0) {
                edit = true;
                comparationId = id;
            }
        }

        HttpSession session = request.getSession();
        ModelAndView mav = new ModelAndView("renderXml");

        DelegateBSC delegate = (DelegateBSC) Factory.getBean("DelegateBSC");
        Comparation comparation = null;


        Framework framework = null;
        if (session.getAttribute("frameworkId") instanceof Long) {

            Long frameworkId = (Long) session.getAttribute("frameworkId");
            framework = (Framework) Factory.getBean("Framework");
            framework = delegate.findFramework(Long.valueOf(frameworkId));

        } else {
            throw new Exception("no hay frameworkId");
        }


        if (edit) {
            comparation = delegate.findComparation(comparationId);
        } else {
            comparation = (Comparation) Factory.getBean("Comparation");
        }


        comparation.setName(request.getParameter("name"));
        comparation.setDescription(request.getParameter("description"));
        comparation.setFramework(framework);
        comparation.setType(Comparation.Type.CUSTOM);

        if (edit) {
            delegate.updateComparation(comparation);
        } else {
            delegate.createComparation(comparation);

            //Se agrega a los indicadores el comparator respectivo al comparation
            List<ScoreCard> scoreCards = delegate.findScoreCard();

            for (ScoreCard scoreCard : scoreCards) {
                if (scoreCard instanceof Pointer) {
                    Comparator comparator = (Comparator) Factory.getBean("Comparator");
                    comparator.setDescription(comparation.getName());
                    comparator.setPointer((Pointer)scoreCard);
                    comparator.setComparation(comparation);
                    delegate.createComparator(comparator);
                    // comparator.setDescription(comparation.getName());

                    //Se crea el settings del comparador
                    //comparator settings

                    Settings comparatorSettings = (Settings) Factory.getBean("Settings");

                    //Esta parte cambiará en fase 2.  Ahorita se agrega siempre suma
                    
                    comparatorSettings.setCommand(Settings.Command.SUM);
                    comparatorSettings.setType(Settings.Type.COMPARATOR);
                    comparatorSettings.setComparator(comparator);
                    delegate.createSettings(comparatorSettings);

                    comparator.setSettings(comparatorSettings);
                    delegate.updateComparator(comparator);

                }

            }






        }

        mav.addObject("id", comparation.getIdComparation());

        return mav;
    }
}
